import map from '../../utils/map.js';
import cityobj from '../../utils/map_p.js';
const app = getApp();
let self;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    styles: "",
    letter: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"],
    cityListId: '',
    allCityList:[],
    //下面是城市列表信息，这里只是模拟数据
    citylist:[],
    //下面是热门城市数据，模拟数据
    newcity: ['北京', '上海', '广州', '深圳', '成都', '杭州'],
    locateCity: '',
    search:'',
    cityListData:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self=this;
    cityobj().then(res=>{
      self.data.citylist=res;
      self.data.allCityList=res;
      self.setData({
        citylist: self.data.citylist
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let  cityOrTime = wx.getStorageSync('locatecity') || {},
      time = new Date().getTime(),
      city = '';
    if (!cityOrTime.time || (time - cityOrTime.time > 1800000)) {//每隔30分钟请求一次定位
      self.getLocate();
    } else {//如果未满30分钟，那么直接从本地缓存里取值
      self.setData({
        locateCity: cityOrTime.city
      })
    }
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowWidth);
        console.log(res.windowHeight);
        self.data.styles = `height:${res.windowHeight}px`;
        self.setData({
          styles: self.data.styles
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  cityTap(e) {
    const val = e.currentTarget.dataset.val || '',
      types = e.currentTarget.dataset.types || '',
      Index = e.currentTarget.dataset.index || '';
      let city = self.data.citySel;
    switch (types) {
      case 'locate':
        //定位内容
        city = self.data.locateCity;
        break;
      case 'national':
        //全国
        city = '全国';
        break;
      case 'new':
        //热门城市
        city = val;
        break;
      case 'list':
        //城市列表
        city = val.fullname;
        break;
    }
    wx.redirectTo({
      url:`/pages/index/index?addr=${city}`
    })
  },
  //点击城市字母
  letterTap(e) {
    const Item = e.currentTarget.dataset.item;
    self.setData({
      cityListId: Item
    });
    console.log(self.data.cityListId);
  },
  //调用定位
  getLocate() {
    map.getLocateInfo().then(res=>{
      self.setData({
        locateCity: res.city
      });
      //把获取的定位和获取的时间放到本地存储
      wx.setStorageSync('locatecity', { city: res.city, time: new Date().getTime()});
    })
  },
  getSearch(e) {
    self.data.search = e.detail.search;
    self.setData({
      search: self.data.search 
    })
    self.getCityList();
  },
  getCityList(){
    self.data.cityListData=[];
    self.data.citylist.forEach(item=>{
      item.data.forEach(i=>{
        if (i.fullname.indexOf(self.data.search)>-1){
          self.data.cityListData.push(i);
        }
      })
    })
    self.setData({
      cityListData: self.data.cityListData
    })
  }
})